﻿'=============================
'【窗体名称】财务入账
'【作者】罗邓
'【功能说明】
'   1. 用户录入财务入账信息（金额、地点、支付人、入账方式、备注）；
'   2. 点击“保存入账”按钮，将数据写入数据库表 RZJLK；
'   3. 自动记录入账时间（RZSJ = GETDATE()）；
'   4. 点击“重置”清空输入框；
'   5. 点击“退出”关闭窗体；
'=============================
Imports System.Data.SqlClient

Public Class 财务入账

    '------------------------------------------
    ' 定义数据库连接字符串（从配置类读取）
    '------------------------------------------
    Private connectionString As String = DbConnectionHelper.GetConnectString()

    '==========================================
    '【1】窗体加载事件：初始化界面控件（如下拉框等）
    '==========================================
    Private Sub 财务入账_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' 设置入账方式下拉框选项
        cboRZFS.Items.Clear()
        cboRZFS.Items.Add("现金")
        cboRZFS.Items.Add("银行转账")
        cboRZFS.Items.Add("微信支付")
        cboRZFS.Items.Add("支付宝")
        cboRZFS.SelectedIndex = 0

        ' 设置默认日期为当前时间
        txtRZSJ.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
    End Sub

    '==========================================
    '【2】保存入账记录
    '==========================================
    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        ' ① 基础校验
        If String.IsNullOrWhiteSpace(txtRZJE.Text) Then
            MessageBox.Show("请输入入账金额！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            txtRZJE.Focus()
            Exit Sub
        End If

        If Not IsNumeric(txtRZJE.Text) Then
            MessageBox.Show("入账金额必须是数字！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txtRZJE.Focus()
            Exit Sub
        End If

        ' ② 获取用户输入的各项值
        Dim RZJE As Double = Convert.ToDouble(txtRZJE.Text)
        Dim DD As String = txtDD.Text.Trim()
        Dim ZFR As String = txtZFR.Text.Trim()
        Dim MEMO As String = txtMEMO.Text.Trim()
        Dim RZFS As Integer = cboRZFS.SelectedIndex + 1   ' 用数字表示方式：1现金、2转账、3微信、4支付宝

        ' ③ 写入数据库
        Try
            Using conn As New SqlConnection(connectionString)
                conn.Open()

                Dim sql As String = "INSERT INTO RZJLK (RZJE, RZSJ, MEMO, DD, RZFS, ZFR) VALUES (@RZJE, GETDATE(), @MEMO, @DD, @RZFS, @ZFR)"
                Using cmd As New SqlCommand(sql, conn)
                    cmd.Parameters.AddWithValue("@RZJE", RZJE)
                    cmd.Parameters.AddWithValue("@MEMO", MEMO)
                    cmd.Parameters.AddWithValue("@DD", DD)
                    cmd.Parameters.AddWithValue("@RZFS", RZFS)
                    cmd.Parameters.AddWithValue("@ZFR", ZFR)
                    cmd.ExecuteNonQuery()
                End Using

                MessageBox.Show("入账记录保存成功！", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information)
                ResetForm() ' 调用清空函数
            End Using

        Catch ex As Exception
            MessageBox.Show("保存入账时发生错误：" & ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    '==========================================
    '【3】重置按钮：清空输入框内容
    '==========================================
    Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
        ResetForm()
    End Sub

    '------------------------------------------
    ' 清空所有输入框内容的辅助方法
    '------------------------------------------
    Private Sub ResetForm()
        txtRZJE.Clear()
        txtDD.Clear()
        txtZFR.Clear()
        txtMEMO.Clear()
        cboRZFS.SelectedIndex = 0
        txtRZSJ.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
    End Sub

    '==========================================
    '【4】退出按钮：关闭窗体
    '==========================================
    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

End Class
